package com.ujs.shc.service.impl;

import com.ujs.shc.dao.ImageMapper;
import com.ujs.shc.pojo.Image;
import com.ujs.shc.service.ImageService;
import com.ujs.shc.util.UtilDate;
import com.ujs.shc.util.UtilImage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;

@Service
public class ImageServiceImpl implements ImageService {

    @Autowired
    ImageMapper imageMapper;

    @Override
    public List<Image> getImagesByGoodsPrimaryKey(Integer goodsId) {
        return imageMapper.selectByGoodsPrimaryKey(goodsId);
    }

    @Override
    public Integer addImage(Image image, HttpServletRequest request, MultipartFile file) {

        try {
            //核心 将multipart拷贝到文件中
//            file.transferTo(saveFile);
            String saveFileName = UtilImage.copyFile(request, file,"upload/goods/");
            image.setImgUrl("/upload/goods/" + saveFileName);
            return imageMapper.insertImage(image);

        } catch (IOException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
